package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.InspectTaskDetailMapper;
import cc.rengu.igas.bomp.common.dao.ManagerInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.InspectTaskDetailMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.ManagerInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.InspectTaskDetail;
import cc.rengu.igas.bomp.common.entity.ManagerInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.MchntInspectInfoManagerRequest;
import cc.rengu.igas.bomp.facade.response.MchntInspectInfoManagerResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/03/31
 */
public class MchntInspectInfoManagerService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntInspectInfoManagerRequest mchntInspectInfoManagerRequest = new MchntInspectInfoManagerRequest();
        ConvertUtil.convertOutput(mchntInspectInfoManagerRequest);
        return mchntInspectInfoManagerRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MchntInspectInfoManagerRequest mchntInspectInfoManagerRequest = (MchntInspectInfoManagerRequest) request;
        MchntInspectInfoManagerResponse mchntInspectInfoManagerResponse = new MchntInspectInfoManagerResponse();
        /*获取请求报文信息:用户标识，巡检任务号，操作类型，审核状态，拒绝原因，巡检类型、巡检机构、拓展经理ID，巡检时间*/
        /*获取法人行号*/
        String instId = mchntInspectInfoManagerRequest.getHeader().getInstId();
        /*获取用户标识*/
        String userId = mchntInspectInfoManagerRequest.getUserId();
        /*获取巡检任务号*/
        String inspectTaskNo = mchntInspectInfoManagerRequest.getInspectTaskNo();
        /*获取商户号*/
        String mchntNo = mchntInspectInfoManagerRequest.getMchntNo();
        /*获取操作类型*/
        String oprType = mchntInspectInfoManagerRequest.getOprType();
        /*获取审核状态*/
        String checkStatus = mchntInspectInfoManagerRequest.getCheckStatus();
        /*获取审核拒绝原因*/
        String auditRefuseReason = mchntInspectInfoManagerRequest.getAuditRefuseReason();
        /*获取巡检方式*/
        String inspectType = mchntInspectInfoManagerRequest.getInspectType();
        /*获取巡检机构*/
        String inspectInstId = mchntInspectInfoManagerRequest.getInspectInstId();
        /*获取拓展经理*/
        String managerId = mchntInspectInfoManagerRequest.getManagerId();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(inspectTaskNo) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(oprType)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if ("03".equals(oprType) && StringUtil.isEmptyOrNull(checkStatus)) {
            rglog.error("审核操作时审核状态不能为空!" + checkStatus);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        if ("03".equals(oprType) && "03".equals(checkStatus) && StringUtil.isEmptyOrNull(auditRefuseReason)) {
            rglog.error("拒绝原因不能为空!" + auditRefuseReason);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        if ("02".equals(oprType) && StringUtil.isEmptyOrNull(inspectType)) {
            rglog.error("巡检方式不能为空!" + auditRefuseReason);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*根据巡检任务号和商户号查询商户巡检任务详情*/
        InspectTaskDetailMapper inspectTaskDetailMapper = new InspectTaskDetailMapperImpl();
        InspectTaskDetail inspectTaskDetail = inspectTaskDetailMapper.selectInspectTaskDetailByPrimaryKey(instId, inspectTaskNo, mchntNo);
        if (null == inspectTaskDetail) {
            rglog.error("查询商户巡检信息失败!" + instId + "----" + inspectTaskNo + "-----------" + mchntNo);
            throw new BizException(RespCodeEnum.MCHNT_INSPECT_INFO_ERROR.getRespCode(), RespCodeEnum.MCHNT_INSPECT_INFO_ERROR.getRespDesc());
        }
        /*修改操作信息赋值*/
        if ("02".equals(oprType)) {
            /* 巡检方式赋值 */
            inspectTaskDetail.setInspectType(inspectType);
            /* 巡检机构赋值 */
            if (!StringUtil.isEmptyOrNull(inspectInstId)) {
                inspectTaskDetail.setInspectInstId(inspectInstId);
            }
            /*巡检拓展经理信息获取赋值（拓展经理与巡检机构关联，当拓展经理修改时巡检机构一定有对应信息上送）*/
            if (!StringUtil.isEmptyOrNull(managerId)) {
                inspectTaskDetail.setManagerId(managerId);
                ManagerInfoMapper managerInfoMapper = new ManagerInfoMapperImpl();
                ManagerInfo managerInfo = managerInfoMapper.selectManagerInfoByPrimaryKey(instId, inspectInstId, managerId);
                if (null != managerInfo) {
                    inspectTaskDetail.setManagerName(managerInfo.getManagerName());
                } else {
                    rglog.error("查询拓展经理信息失败!" + instId + "----" + inspectInstId + "-----------" + managerId);
                    throw new BizException(RespCodeEnum.QUERY_MANAGER_INFO_ERROR.getRespCode(), RespCodeEnum.QUERY_MANAGER_INFO_ERROR.getRespDesc());
                }
            }
            /*巡检状态赋值（巡检方式为线上审核巡检，将巡检状态改为02-待审核）*/
            if ("10".equals(inspectType)) {
                inspectTaskDetail.setInspectStatus("02");
            } else {
                inspectTaskDetail.setInspectStatus(checkStatus);
            }
        } else if ("03".equals(oprType)) {
            if ("03".equals(checkStatus)) {
                /*拒绝原因赋值*/
                inspectTaskDetail.setInspectRefuseReason(auditRefuseReason);
            }
            /*审核时间赋值*/
            inspectTaskDetail.setInspectAuditTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
            /*最后审核人员Id赋值*/
            inspectTaskDetail.setLastAuditId(userId);
            /*最后审核时间赋值*/
            inspectTaskDetail.setLastAuditTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
            /*巡检状态赋值*/
            inspectTaskDetail.setInspectStatus(checkStatus);
        } else {
            rglog.error("操作类型信息错误!" + oprType);
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        inspectTaskDetail.setLastOprId(userId);
        inspectTaskDetail.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
        int iReturnCode = inspectTaskDetailMapper.updateInspectTaskDetailById(inspectTaskDetail);
        if (0 != iReturnCode) {
            rglog.error("更新巡检任务详情失败!" + iReturnCode + "------" + inspectTaskDetail.getId());
            throw new BizException(RespCodeEnum.UPDATE_INSPECT_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_INSPECT_INFO_ERROR.getRespDesc());
        }
        BizResponse<MchntInspectInfoManagerResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntInspectInfoManagerResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntInspectInfoManagerResponse mchntInspectInfoManagerResponse = (MchntInspectInfoManagerResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntInspectInfoManagerResponse);
    }
}
